﻿//********************************************************************************//
//创建日期：<创建日期，2016-12-14>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：应急预案
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Http;
using System.Text;
using Web.Class;
using DevExpress.Web;
using System.IO;

namespace Web.Pages.YongShui.YingJiYA
{
    public partial class YingJiYA : BasePage
    {
        #region 属性

        /// <summary>
        /// 当前操作项ID
        /// </summary>
        private int XingZhengQID
        {
            get
            {
                return Convert.ToInt32(ViewState["_XingZhengQID"]);
            }
            set
            {
                ViewState["_XingZhengQID"] = value;
            }
        }
        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        private string UploadDirectory = "/UPLOAD/YJ/";  //图片上传文件夹路径

        #endregion

        #region 变量

        /// <summary>
        /// 应急预案对象
        /// </summary>
        YS_YJ_YINGJIYA _YS_YJ_YINGJIYA = new YS_YJ_YINGJIYA();

        /// <summary>
        /// 应急预案BLL
        /// </summary>
        YS_YJ_YINGJIYAProcess _YS_YJ_YINGJIYABLL = new YS_YJ_YINGJIYAProcess();

        /// <summary>
        /// 附件BLL
        /// </summary>
        YS_YJ_FUJIANProcess fjbll = new YS_YJ_FUJIANProcess();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitData();   
            }
            LoadData();
        }

        /// <summary>
        /// 绑定空表头
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            base.Render(sender);
        }

       

        /// <summary>
        /// 响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            CallbackResult cr = new CallbackResult(e.Parameter);
            int id = Convert.ToInt32(this.hdCollection.Get("ID"));
            string savePath = Server.MapPath(UploadDirectory);
            string fullPath =null;
            if (e.Parameter == "info")
            {      
                this._YS_YJ_YINGJIYA = this._YS_YJ_YINGJIYABLL.SelectYS_YJ_YINGJIYA(id);
                //获取附件
                DataRowCollection drctemp = fjbll.SelectYS_YJ_FUJIANsDynamic("I_JLZT=0 AND I_YINGJIYAID=" + id, null).Rows;
                //string[] idArr = new string[drctemp.Count];
                string[] fnArr = new string[drctemp.Count];
                string[] fnserverArr = new string[drctemp.Count];
                string[] fsArr = new string[drctemp.Count]; 
                for (int i = 0; i < drctemp.Count; i++)
                {
                    //idArr[i] = drctemp[i]["ID"].ToString();
                    fnArr[i] = drctemp[i]["S_FILENAME"].ToString();
                    fnserverArr[i] = drctemp[i]["S_FILESERVERNAME"].ToString();
                    fsArr[i] = drctemp[i]["S_FILESIZE"].ToString();
                }

                
                cr.IsSuccess = true;
                cr.IsAlertMsg = false;
                cr.AddValue(this._YS_YJ_YINGJIYA.S_TITLE,  string.Join(",", fnArr), string.Join(",", fnserverArr), string.Join(",", fsArr));
                e.Result = cr.ToString();
                return;
            }
            else if (e.Parameter == "del")
            {
                _YS_YJ_YINGJIYA = _YS_YJ_YINGJIYABLL.SelectYS_YJ_YINGJIYA(id);
                //执行逻辑删除            
                _YS_YJ_YINGJIYA.S_XIUGAIR = LoginInfo.CurrentUser.Account;
                _YS_YJ_YINGJIYA.D_XIUGAISJ = BusinessTool.GetDataBaseDateTime();
                _YS_YJ_YINGJIYA.I_JLZT = -1;
                _SYS_LOGBLL.Log<YS_YJ_YINGJIYA>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "应急预案", OperateState.Delete, _YS_YJ_YINGJIYA, null);
                if (_YS_YJ_YINGJIYABLL.InsertOrUpdate(_YS_YJ_YINGJIYA))
                {
                                       
                    //获取附件文件在服务器上的文件名
                    DataRowCollection drctemp = fjbll.SelectYS_YJ_FUJIANsDynamic("I_JLZT=0 AND I_YINGJIYAID=" + id, null).Rows;
                    //删除文件
                    foreach (DataRow itemT in drctemp)
                    {
                        fullPath = Path.Combine(savePath, itemT["S_FILESERVERNAME"].ToString());
                        if (System.IO.File.Exists(fullPath))
                        {
                            System.IO.File.Delete(fullPath);
                        }                        
                    }
                    //删除数据库附件记录
                    fjbll.DeleteYS_YJ_FUJIANsDynamic("I_YINGJIYAID=" + _YS_YJ_YINGJIYA.ID.ToString(), null);
                    cr.IsSuccess = true;
                    cr.IsAlertMsg = true;
                    cr.Msg = "操作成功！";
                    e.Result = cr.ToString();
                    return;
                }
            }
            else if (e.Parameter=="delFJ")
            { 
                string fjServerName=hdCollection.Get("FJName").ToString();
                fullPath = Path.Combine(savePath, fjServerName);
                if (System.IO.File.Exists(fullPath))
                {
                    System.IO.File.Delete(fullPath);
                }
                cr.IsSuccess = true;
                cr.IsAlertMsg = false;
                cr.Msg = "操作成功！";
                e.Result = cr.ToString();
                return;
            }
            else if (e.Parameter == "save")
            {
                OperateState operateState;
                //添加
                if (id == -1)
                {
                    _YS_YJ_YINGJIYA.ID = 0;
                    _YS_YJ_YINGJIYA.I_JLZT = 0;
                    operateState = OperateState.Add;
                }
                //修改
                else
                {
                    this._YS_YJ_YINGJIYA = _YS_YJ_YINGJIYABLL.SelectYS_YJ_YINGJIYA(id);
                    operateState = OperateState.Modify;
                }
                _YS_YJ_YINGJIYA.S_TITLE = txtBianHao.Text;
                _YS_YJ_YINGJIYA.I_JLZT = 0;
                if (operateState == OperateState.Add)
                {
                    this._YS_YJ_YINGJIYA.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                    this._YS_YJ_YINGJIYA.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
                }
                this._YS_YJ_YINGJIYA.S_XIUGAIR = LoginInfo.CurrentUser.UserID.ToString();
                this._YS_YJ_YINGJIYA.D_XIUGAISJ = BusinessTool.GetDataBaseDateTime();


                _SYS_LOGBLL.Log<YS_YJ_YINGJIYA>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "应急预案", operateState, _YS_YJ_YINGJIYA, null); if (_YS_YJ_YINGJIYABLL.InsertOrUpdate(_YS_YJ_YINGJIYA))
                {
                   
                    cr.IsSuccess = true;
                    cr.IsAlertMsg = true;
                    cr.Msg = "保存成功！";
                    cr.SpecialIndetity = _YS_YJ_YINGJIYA.ID.ToString();
                    e.Result = cr.ToString();
                    //删除旧附件信息
                    YS_YJ_FUJIANProcess fjbll = new YS_YJ_FUJIANProcess();
                    fjbll.DeleteYS_YJ_FUJIANsDynamic("I_YINGJIYAID=" + _YS_YJ_YINGJIYA.ID.ToString(), null);
                    //添加新附件信息
                    if (!string.IsNullOrEmpty(Convert.ToString(hdCollection.Get("fnStr"))))
                    {
                        string[] fnArr = hdCollection.Get("fnStr").ToString().Split(',');
                        string[] fnserverArr = hdCollection.Get("fnserverStr").ToString().Split(',');
                        string[] fsArr = hdCollection.Get("fsStr").ToString().Split(',');
                        for (int i = 0; i < fnArr.Length; i++)
                        {
                            YS_YJ_FUJIAN itemT = new YS_YJ_FUJIAN();
                            itemT.I_YINGJIYAID = _YS_YJ_YINGJIYA.ID;
                            itemT.S_FILENAME = fnArr[i];
                            itemT.S_FILESERVERNAME = fnserverArr[i];
                            itemT.S_FILESIZE = Convert.ToDecimal(fsArr[i]);
                            itemT.S_CAOZUOR = this._YS_YJ_YINGJIYA.S_CAOZUOR;
                            itemT.D_CAOZUOSJ = this._YS_YJ_YINGJIYA.D_XIUGAISJ;
                            itemT.I_JLZT = 0;
                            fjbll.InsertOrUpdate(itemT);
                        }
                    }
                    return;
                }
            }
            
        }

        protected void uplImage_FileUploadComplete(object sender, FileUploadCompleteEventArgs e)
        {            
            if (this.UpLoadFile.UploadedFiles.Length==0)
            {
                e.CallbackData = "";
                return;
            }

            string fileName = Guid.NewGuid() + Path.GetExtension(this.UpLoadFile.UploadedFiles[0].FileName);  //文件名
            string filePath = Path.Combine(Server.MapPath(UploadDirectory), fileName);    //文件完整物理路径
            if (!System.IO.Directory.Exists(Server.MapPath(UploadDirectory)))
            {
                System.IO.Directory.CreateDirectory(Server.MapPath(UploadDirectory));
            }
            this.UpLoadFile.UploadedFiles[0].SaveAs(filePath);
            e.CallbackData = string.Join(",", this.UpLoadFile.UploadedFiles[0].FileName, fileName, this.UpLoadFile.UploadedFiles[0].ContentLength);
        }

        protected void gvList_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData();
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            this.dtSource = new SpHelper().P_YS_YINGJIYAHQU().Tables[0];
            gvList.DataSource = this.dtSource;
            gvList.DataBind();
        }

        #endregion        
    }
}